﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class CommentProductsDAO
    {
        #region Fields

        protected string connectionStringName;

        #endregion
        /// <summary>
        /// Saves a record to the CommentProducts table.
        /// </summary>
        public virtual int Insert(CommentProductsInfo commentProductsInfo)
        {
            try
            {

                return new CommentProductsDAO().Insert(commentProductsInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Saves a record to the CommentProducts table.
        /// </summary>
        public virtual void Update(CommentProductsInfo commentProductsInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", commentProductsInfo.Id),
                new SqlParameter("@ProductId", commentProductsInfo.ProductId),
                new SqlParameter("@Title", commentProductsInfo.Title),
				new SqlParameter("@Contents", commentProductsInfo.Contents),
				new SqlParameter("@CreateDate", commentProductsInfo.CreateDate),
			    new SqlParameter("@Status", commentProductsInfo.Status),	
				new SqlParameter("@CodeMember", commentProductsInfo.CodeMember)			
				
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CommentProducts_Update", parameters);

        }

        /// <summary>
        /// Delete a record from the CommentProducts table.
        /// </summary>
        public virtual void Delete(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CommentProducts_Delete", parameters);

        }

        // <summary>
        //  Selects a single record from the Singer table.
        //   </summary>
        public virtual CommentProductsInfo Select(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id", id)
            };

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CommentProducts_Select", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeCommentProductInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// Selects all records from the CommentProducts table.
        /// </summary>
        public CHRTList<CommentProductsInfo> SelectAll()
        {
            try
            {
                return new CommentProductsDAO().SelectAll();
            }
            catch (Exception ex)
            {
                throw;
            }

        }


        //Lấy tổng số bản ghi
        public int GetCount(int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@Status",status)
                                                                                           
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CommentProducts_GetCount", parameters);

        }


        public virtual int SelectCount(int status,int productId, string starDatetime, string endDatetime)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
               new SqlParameter("@Status", status),
               new SqlParameter("@productId", productId),
               new SqlParameter("@starDatetime", starDatetime),
               new SqlParameter("@endDatetime", endDatetime),
               new SqlParameter("@IsTotalRow", 1)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CommentProduct_Search", parameters);
        }

        public virtual DataTable Search(int status,int productId, string starDatetime, string endDatetime,int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@Status", status),
                new SqlParameter("@productId", productId),
                new SqlParameter("@starDatetime", starDatetime),
                new SqlParameter("@endDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", 0)
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "CommentProduct_Search", parameters);
        }

        //hiện thị danh sách bình luận khi search

        public DataSet ListCommentProductsPage(int status, DateTime createOnFrom, DateTime createOnTo, int currPage, int recodperPage, int pageSize, int total, string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                            new SqlParameter("@Status", status),
                                              new SqlParameter("@CreatedOnFrom",createOnFrom),
                                              new SqlParameter("@CreatedOnTo",createOnTo),
                                              new SqlParameter("@currPage", currPage),
                                              new SqlParameter("@recodperPage", recodperPage),
                                              new SqlParameter("@pageSize", pageSize), 
                                              new SqlParameter("@total", total),
                                              new SqlParameter("@link", link)
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "CommentProducts_search_Page", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }

        //Bind dữ liệu vào Dropdownlist
        public DataSet BindDropdownlist(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@Id", id)
                                            
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "CommentProducts_BindDropDownList", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }
        //Update lại trạng thái 
        public virtual void UpdateStatus(int id, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id),
                new SqlParameter("@Status", status),
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CommentProducts_UpdateStatus", parameters);
        }


        /// <summary>
        /// Creates a new instance of the Singer class and populates it with data from the specified SqlDataReader.
        /// </summary>
        protected virtual CommentProductsInfo MakeCommentProductInfo(SqlDataReader dataReader)
        {
            CommentProductsInfo commentproductInfo = new CommentProductsInfo();
            commentproductInfo.Id = SqlClientUtility.GetInt32(dataReader, DbConstants.COMMENTPRODUCTS.ID, 0);
            commentproductInfo.ProductId = SqlClientUtility.GetInt32(dataReader, DbConstants.COMMENTPRODUCTS.PRODUCTID, 0);
            commentproductInfo.Title = SqlClientUtility.GetString(dataReader, DbConstants.COMMENTPRODUCTS.TITLE, String.Empty);
            commentproductInfo.Contents = SqlClientUtility.GetString(dataReader, DbConstants.COMMENTPRODUCTS.CONTENTS, String.Empty);
            commentproductInfo.CreateDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.COMMENTPRODUCTS.CREATEDATE, DateTime.Now);
            commentproductInfo.Status = SqlClientUtility.GetInt32(dataReader, DbConstants.COMMENTPRODUCTS.STATUS, 0);
            commentproductInfo.CodeMember = SqlClientUtility.GetString(dataReader, DbConstants.COMMENTPRODUCTS.CODEMEMBER, String.Empty);

            return commentproductInfo;
        }


    }
}
